package com.weijianhuawen.wjblog.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.weijianhuawen.wjblog.admin.service.AdminStatisticsService;
import com.weijianhuawen.wjblog.common.domain.dos.ArticleCategoryRelDo;
import com.weijianhuawen.wjblog.common.domain.dos.ArticleTagRelDo;
import com.weijianhuawen.wjblog.common.domain.dos.CategoryDo;
import com.weijianhuawen.wjblog.common.domain.dos.TagDo;
import com.weijianhuawen.wjblog.common.domain.mapper.ArticleCategoryRelMapper;
import com.weijianhuawen.wjblog.common.domain.mapper.ArticleTagRelMapper;
import com.weijianhuawen.wjblog.common.domain.mapper.CategoryMapper;
import com.weijianhuawen.wjblog.common.domain.mapper.TagMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-11-08 12:36
 */

@Service
@Slf4j
public class AdminStatisticsServiceImpl implements AdminStatisticsService {
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private ArticleCategoryRelMapper articleCategoryRelMapper;
    @Resource
    private TagMapper tagMapper;
    @Resource
    private ArticleTagRelMapper articleTagRelMapper;
    // 统计分类下文章总数
    @Override
    public void statisticsCategoryArticleTotal() {
        // 查询所有分类
        List<CategoryDo> categoryDos = categoryMapper.selectList(Wrappers.emptyWrapper());

        // 查询分类文章关联记录
        List<ArticleCategoryRelDo> articleCategoryRelDos = articleCategoryRelMapper.selectList(Wrappers.emptyWrapper());
        // 按照分类id 进行分组
        Map<Long, List<ArticleCategoryRelDo>> categoryIdAndArticleCategoryRelMap = new HashMap<>();

        if (!CollectionUtils.isEmpty(articleCategoryRelDos)) {
            categoryIdAndArticleCategoryRelMap = articleCategoryRelDos.stream()
                    .collect(Collectors.groupingBy(ArticleCategoryRelDo::getCategoryId));
        }

        // 统计每个分类下的文章数
        if (!CollectionUtils.isEmpty(categoryDos)) {
            for (CategoryDo categoryDo : categoryDos) {
                // 遍历分类列表
                Long categoryId = categoryDo.getId();
                // 获取映射记录
                List<ArticleCategoryRelDo> articleCategoryRelDoList = categoryIdAndArticleCategoryRelMap.get(categoryId);
                // 获取该分类下的文章总数
                Integer articleTotalCount = CollectionUtils.isEmpty(articleCategoryRelDoList) ? 0 : articleCategoryRelDoList.size();

                // 更新该分类下的文章总数字段
                CategoryDo updateCategoryDo = CategoryDo.builder()
                        .articlesTotal(articleTotalCount)
                        .id(categoryId)
                        .build();

                categoryMapper.updateById(updateCategoryDo);
            }
        }
    }

    @Override
    public void statisticsOneCategoryArticleTotal(Long categoryId) {
        // 根据分类id 查询分类
        CategoryDo categoryDo = categoryMapper.selectById(categoryId);

        if (Objects.nonNull(categoryDo)) {
            // 查询关联的文章
            List<ArticleCategoryRelDo> articleCategoryRelDos = articleCategoryRelMapper.selectListByCategory(categoryId);
            // 获取关联记录数 由于一篇文章只能绑定一个分类 所以分类所关联的文章记录数就是文章总数
            Integer articlesTotalCount = CollectionUtils.isEmpty(articleCategoryRelDos) ? 0 : articleCategoryRelDos.size();
            categoryDo.setArticlesTotal(articlesTotalCount);
            // 更新
            categoryMapper.updateById(categoryDo);
        }
    }

    @Override
    public void statisticsTagArticleTotal() {
        // 查询所有的标签
        List<TagDo> tagDos = tagMapper.selectList(Wrappers.emptyWrapper());

        // 查询所有标签-文章关联记录
        List<ArticleTagRelDo> articleTagRelDos = articleTagRelMapper.selectList(Wrappers.emptyWrapper());

        // 按照 标签id 分类 并建立映射关系
        Map<Long, List<ArticleTagRelDo>> tagIdAndArticleTagRelMap = new HashMap<>();

        if (!CollectionUtils.isEmpty(articleTagRelDos)) {
            tagIdAndArticleTagRelMap = articleTagRelDos.stream()
                    .collect(Collectors.groupingBy(ArticleTagRelDo::getTagId));
        }
        if (!CollectionUtils.isEmpty(tagDos)) {
            for (TagDo tagDo : tagDos) {
                // 遍历tag 更新articles total 字段
                Long tagId = tagDo.getId();

                List<ArticleTagRelDo> articleTagRelDoList = tagIdAndArticleTagRelMap.get(tagId);

                Integer articlesTotalCount = CollectionUtils.isEmpty(articleTagRelDoList) ? 0 : articleTagRelDoList.size();

                // 更新文章总数字段
                TagDo updateTagDo = TagDo.builder()
                        .id(tagId)
                        .articlesTotal(articlesTotalCount)
                        .build();

                // 更新
                tagMapper.updateById(updateTagDo);
            }
        }
    }

    @Override
    public void statisticsSomeTagsArticleTotal(List<Long> tagIds) {
        // 查询标签列表
        List<TagDo> tagDos = tagMapper.selectListByTagIds(tagIds);
        if (!CollectionUtils.isEmpty(tagDos)) {
            // 更新对应tagid 下的 文章总数
            for (TagDo tagDo : tagDos) {
                Long tagId = tagDo.getId();

                // 查询该标签关联
                List<ArticleTagRelDo> articleTagRelDos = articleTagRelMapper.selectListByTagId(tagId);

                // 更新文章总数
                Integer articlesTotalCount = CollectionUtils.isEmpty(articleTagRelDos) ? 0 : articleTagRelDos.size();
                TagDo updateTagDo = TagDo.builder()
                        .id(tagId)
                        .articlesTotal(articlesTotalCount)
                        .build();
                // 更新
                tagMapper.updateById(updateTagDo);
            }
        }
    }
}
